<?php

namespace app\index\controller\lawsuit;

use app\common\model\ConsultingServer;
use app\common\model\ConsultingServerSku;
use app\index\dao\LawsuitCommentDao;
use app\index\dao\MemberDao;
use ShidianUtil\Thinkphp\Response\ApiResponse;
use think\APP;
use think\exception\ValidateException;
use think\Request as Request;

/**
 * Step1 控制器
 * Class Step1
 * @package app\index\controller\lawsuit;
 * @link https://www.kancloud.cn/manual/thinkphp6_0/1037509
 * @see \app\index\validate\lawsuit\LawCase
 */
class Step3
{
    // 请求参数
    private Request $request;
    private MemberDao $memberDao;
    private LawsuitCommentDao $lawsuitCommentDao;

    public function __construct(Request $request, MemberDao $memberDao, LawsuitCommentDao $lawsuitCommentDao)
    {
        $this->request = $request;
        $this->memberDao = $memberDao;
        $this->lawsuitCommentDao = $lawsuitCommentDao;
    }

    /**
     * 读取
     * @return ApiResponse
     * @url /lawsuit/step3/read
     */
    public function read(): ApiResponse
    {
        $request = $this->request;

        $id = $request->param('id');
        if (!is_id($id)) {
            throw new ValidateException('请选择一个案件');
        }

        $lawsuit = \app\common\model\Lawsuit::where('id', $id)
            ->with(['category'])
            ->find();

        $categoryId = $lawsuit->categoryId;
        $cityId = $lawsuit->cityId;
        $provinceId = $lawsuit->provinceId;

        // 案件类型
        $category = $lawsuit->category;

        // 律师
        $lawyers = $this->memberDao->selectLawyersLawsuit($categoryId, $cityId, $provinceId, 4);

        // 服务
        // $server = ConsultingServer::where('serverType', ConsultingServer::QUICK)
        //     ->field(['id', 'showPrice', 'price'])
        //     ->withAttr([
        //         'showPrice' => fn($value) => (float)$value,
        //         'price' => fn($value) => (float)$value,
        //     ])
        //     ->find();

        // 服务
        $server = ConsultingServer::where('serverType', ConsultingServer::PHONE)
            ->field(['id', 'showPrice', 'price', 'name', 'icon'])
            ->withAttr([
                'price' => fn($value) => (float)$value,
                'showPrice' => fn($value) => (float)$value,
            ])
            ->append(['iconImg'])
            ->find();
        $server->consultingServerSkuId = '0';

        $servers = $this->getServers();

        // 评论
        $comments = $this->lawsuitCommentDao->selectCommentsLawsuit($categoryId);
        if ($comments->isEmpty()) {
            $comments = $this->lawsuitCommentDao->selectCommentsLawsuit(0);
        }

        // 倒计时
        $ts = 3600;

        $data = [
            'id' => $id,
            'ts' => $ts,
            'category' => $category,
            'lawyers' => $lawyers,
            'server' => $server,
            'servers' => $servers,
            'messages' => [
                [
                    'timeStr' => '4分钟前',
                    'content' => '吴律师帮助丁先生追回欠款86000元',
                ],
                [
                    'timeStr' => '7分钟前',
                    'content' => '田律师帮助张女士追回欠款54000元',
                ],
                [
                    'timeStr' => '12分钟前',
                    'content' => '庞律师帮助李先生追回欠款120000元',
                ],
                [
                    'timeStr' => '2分钟前',
                    'content' => '田律师帮助吴先生获得拆迁款250000元',
                ],
                [
                    'timeStr' => '7分钟前',
                    'content' => '周律师帮助苟先生获得拆迁款400000元',
                ],
                [
                    'timeStr' => '5分钟前',
                    'content' => '田律师帮助邱女士追回违约金7500元',
                ],
                [
                    'timeStr' => '6分钟前',
                    'content' => '吕律师帮助周先生追回违约金8000元',
                ],
                [
                    'timeStr' => '11分钟前',
                    'content' => '吴律师帮助夏先生获得劳动赔偿金8643元',
                ],
                [
                    'timeStr' => '3分钟前',
                    'content' => '周律师帮助张先生获得劳动赔偿金6400元',
                ],
                [
                    'timeStr' => '4分钟前',
                    'content' => '李律师帮助刘女士医疗赔偿8000元',
                ],
            ],
            'comments' => $comments,
        ];

        return ApiResponse::success($data);
    }

    private function getServers(){

        $servers = [];

        $server = ConsultingServer::where('serverType', ConsultingServer::PHONE)
            ->field(['id', 'showPrice', 'price', 'name', 'icon'])
            ->withAttr([
                'price' => fn($value) => (float)$value,
                'showPrice' => fn($value) => (float)$value,
            ])
            ->append(['iconImg'])
            ->find();
        $server->consultingServerSkuId = '0';

        $servers[] = $server;

        // 查询SKU
        ConsultingServerSku::where('consultingServerId', $server->id)
            ->where('deleted', '0')
            ->withAttr([
                'price' => fn($value) => (float)$value,
                'showPrice' => fn($value) => (float)$value,
            ])
            ->select()
            ->each(function (ConsultingServerSku $sku) use (&$servers, $server) {

                $temp = clone $server;

                foreach ($temp->getData() as $field => $value) {
                    if (isset($sku->$field)) {
                        $temp->$field = $sku->$field;
                    }
                }

                $temp->consultingServerSkuId = $sku->id;
                $temp->id = $server->id;

                $servers[] = $temp;
            });

        return $servers;
    }
}
